﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace dahtd4ever
{
    public class LevelManager
    {
        List<Level> levels;
        public List<Level> getLevels() { return this.levels; }
        public void setLevels(List<Level> levels) { this.levels = levels; }
        Level level;
        List<Wave> waves;
        Wave wave;
        int x;

        public LevelManager()
        {
        }

        public void Init()
        {
            levels = new List<Level>();
            level = new Level();
            waves = new List<Wave>();
            wave = new Wave();
        }


        //Dateipfad finden
        internal static string GetProjectPath()
        {
            // Ergebnis: Debug- oder Release-Ordner im Projektordner.
            string projectPath = Environment.CurrentDirectory;
            // Mit jedem Durchlauf geht es im Verzeichnisbaum eine Stufe höher.
            for (int i = 0; i <= 4; i++)
            {
                projectPath = System.IO.Path.GetDirectoryName(projectPath);
            }
            return projectPath + @"\";
        }


        public void readInLevels(int levelCount)
        {
            string levelString1 = "Levels/0";
            string levelString2 = ".level";
            string levelStringCombined;
            for (int i = 1; i < levelCount + 1; i++)
            {
                if (i >= 9) { levelString1 = "Levels/"; }
                levelStringCombined = levelString1 + i + levelString2;
                readInLevel(levelStringCombined);
                levels.Add(level);
            }
        }

        public bool readInLevel(string path)
        {
            level = new Level();
            waves = new List<Wave>();

            System.IO.StreamReader file;

            String currentDirectory = GetProjectPath();

            // Map-Datei öffnen
            file = new System.IO.StreamReader(Path.Combine(currentDirectory, path));

            file.ReadLine(); // Levelid folgt
            level.setId(Convert.ToInt32(file.ReadLine()));
            file.ReadLine();//Geschafft?
            int yxc = Convert.ToInt32(file.ReadLine());
                if (yxc == 0) { level.setCompleted(false); }
                else { level.setCompleted(true); }  
            file.ReadLine(); // Beschreibungsstring
            level.setDescription(file.ReadLine());
            file.ReadLine(); //Mappfad folgt
            level.setMapDirectory(Convert.ToString(file.ReadLine()));
            file.ReadLine(); //Resourcen folgt
            level.setResources((float)Convert.ToInt32(file.ReadLine()));
            file.ReadLine(); //Leben folgt
            level.setPlayersLife((float)Convert.ToInt32(file.ReadLine()));
            file.ReadLine(); // Turmtypen
            int toweranzahl = Convert.ToInt32(file.ReadLine());
            for (int i = 0; i < toweranzahl; i++)
            {
                level.addPossibleTower(new Tower(Convert.ToInt32(file.ReadLine())));
            }
            file.ReadLine(); // folgend SpawnTime
            level.setEnemySpawnTime(Convert.ToInt32(file.ReadLine()) / 1000);
            file.ReadLine(); // folgend Wellenzahl
            level.setWaveCount(Convert.ToInt32(file.ReadLine()));
            // Gegnertypen einlesen
            //int enemiesInNumber;
            for (int i = 0; i < level.getWaveCount(); i++)
            {
                wave = new Wave();
                file.ReadLine();
                //wave.setEnemyCount(Convert.ToInt32(file.ReadLine()));

                // neue Liste für Gegner
                List<int> enemiesInWave = new List<int>();

                // die Zeile einlesen
                String s = Convert.ToString(file.ReadLine());

                String tmp = "";

                // die Zeichenkette durchgehen
                for (int j = 0; j < s.Length; j++)
                {
                    Char c = s.ElementAt(j);
                    if (c != ',')
                    {
                        tmp = tmp + Convert.ToString(c);
                    }
                    if (c == ',')
                    {
                        enemiesInWave.Add(Convert.ToInt32(tmp));
                        tmp = "";
                    }
                }

                wave.setEnemies(enemiesInWave);
                wave.setEnemyCount(enemiesInWave.Count);
                waves.Add(wave);
            }
            level.setWaves(waves);


            // Datei schließen
            file.Close();

            return true;
        }

        private List<int> enemyListOutOfNumber(int input, Wave wave)
        {
            List<int> list = new List<int>();
            // erstmal die Zahl in Ziffern zerlegen und in Array speichern
            int[] enemyNumbers = new int[wave.getEnemyCount()];
            enemyNumbers = Extract(input);

            // Dann alle Ziffern auf Gegnerliste der Welle schreiben
            for (int i = 0; i < wave.getEnemyCount(); i++)
            {
                list.Add(enemyNumbers[i]);
            }
            return list;
        }

        public static int[] Extract(int number)
        {
            int multipleOfTen = 1;
            int digitCount = 1;
            while (number / multipleOfTen >= 10.0f)
            {
                multipleOfTen *= 10;
                ++digitCount;
            }
            int[] digits = new int[digitCount];
            for (int i = 0; multipleOfTen > 0; ++i, multipleOfTen /= 10)
            {
                digits[i] = (int)(number / multipleOfTen);
                number -= digits[i] * multipleOfTen;
            }

            return digits;
        }
    }
}
